package com.car.controller.yearcheck;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.dao.user.UserDao;
import com.car.entity.YearCheck;
import com.car.service.impl.yearcheck.YearCheckServiceImpl;
import com.car.util.ResultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

@RestController
public class YearCheckController {

    @Autowired
    YearCheckServiceImpl yearCheckServiceImpl;

    @Autowired
    UserDao userDao;

    @RequestMapping("/toYearCheckList")
    public ModelAndView toYearCheckList(){
        return new ModelAndView("yearcheck-list");
    }

    @ResponseBody
    @RequestMapping("/yearCheckList")
    public Map yearCheckList(String carIdCard, String yearCheckStart, String yearCheckEnd, String PageNum, String PageSize) {
        Integer pageNum = Integer.valueOf(PageNum);
        Integer pageSize = Integer.valueOf(PageSize);

        Map<String, Object> map = new HashMap<>();
        map.put("carIdCard", carIdCard);
        map.put("yearCheckStart", yearCheckStart);
        map.put("yearCheckEnd", yearCheckEnd);
        map.put("pageNum", pageNum);
        map.put("pageSize", pageSize);

        Page page = yearCheckServiceImpl.selectLikeYearCheck(map);
        map.put("page", page);
        map.put("totalPage", page.getTotal() / page.getSize() == 0 ?
                page.getTotal() / page.getSize() :
                (page.getTotal() / page.getSize() + 1)
        );
        return map;
    }

    @ResponseBody
    @RequestMapping("/toAddYearCheck")
    public ModelAndView toAddYearCheck() {
        return new ModelAndView("yearcheck-add");
    }

    @ResponseBody
    @RequestMapping("/addYearCheck")
    public ResultMsg addYearCheck(YearCheck yearCheck, String userName) {
        ResultMsg resultMsg = yearCheckServiceImpl.addYearCheck(yearCheck, userName);
        return resultMsg;
    }

    @ResponseBody
    @RequestMapping("/delYearCheck")
    public ResultMsg delYearCheck(YearCheck yearCheck) {
        ResultMsg resultMsg = yearCheckServiceImpl.delYearCheck(yearCheck);
        return resultMsg;
    }

    @ResponseBody
    @RequestMapping("/updateYearCheck")
    public ResultMsg updateYearCheck(YearCheck yearCheck, String userName) {
        ResultMsg resultMsg = yearCheckServiceImpl.updateYearCheck(yearCheck);
        return resultMsg;
    }

    @ResponseBody
    @RequestMapping("/toUpdateYearCheck")
    public ModelAndView toUpdateYearCheck(YearCheck yearCheck) {
        ModelAndView mv = new ModelAndView();
        ResultMsg resultMsg = yearCheckServiceImpl.selectById(yearCheck);
        mv.setViewName("yearcheck-edit");
        mv.addObject("result", resultMsg);
        return mv;
    }
}
